﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using WebRSSBuilder.Helpers;
using WebRSSBuilder.ServiceReferenceWCFRSS;
using System.Windows.Data;
using WebRSSBuilder.Web;

namespace WebRSSBuilder.Models
{
    public class RSSViewModel: INotifyPropertyChanged
    {
        private RSSFeed feed;
        private bool isChanged;
        public bool IsChanged
        {
            get
            {
                return isChanged;
            }
            set
            {
                this.isChanged = value;
                LoadCommand.CanExecute(null);
                SaveCommand.CanExecute(null);
                NotifyPropertyChanged("IsChanged");
            }
        }
        WCFRSSDataSourceClient client; 
        public DelegateCommand LoadCommand {get; set;}
        private PagedCollectionView itemsPaged; 
        public PagedCollectionView ItemsPaged 
        { 
            get 
            {
                return this.itemsPaged; 
             }
            set 
            {
                this.itemsPaged = value;
                this.itemsPaged.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(itemsPaged_CollectionChanged);
                this.NotifyPropertyChanged("ItemsPaged");
              
            }
        }
        public DelegateCommand SaveCommand { get; set; } 
        public RSSFeed Feed { 
            get { return this.feed; } 
            set 
            { 
                this.feed = value;
                if (feed != null)
                {
                    this.feed.PropertyChanged += new PropertyChangedEventHandler(feed_PropertyChanged);
                    if (feed.Items != null)
                        this.ItemsPaged = new PagedCollectionView(this.feed.Items);
                }
                NotifyPropertyChanged("Feed"); 
            
            }
        }

        void feed_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            IsChanged = true; 
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string PropertyName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
                
            }
        }
        void itemsPaged_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            IsChanged = true;
        }
        public RSSViewModel()
        {
            isChanged = false;
            LoadCommand = new DelegateCommand(LoadRSS, (obj) => { return IsChanged; });
            SaveCommand = new DelegateCommand(SaveRSS, (obj) => { return IsChanged; });
            LoadCommand.Execute(null);
            
        }


        private void LoadRSS(object Parameter)
        {

            client = new WCFRSSDataSourceClient("CustomBinding_WCFRSSDataSource1");
            client.GetRssFeedCompleted += new EventHandler<GetRssFeedCompletedEventArgs>(client_GetRssFeedCompleted);
            client.GetRssFeedAsync(((File)App.Current.Resources["File"]).FileID); 
            
        }

        public void SaveRSS(object Parameter)
        {
            client = new WCFRSSDataSourceClient("CustomBinding_WCFRSSDataSource1");
            client.UpdateRssFeedCompleted +=new EventHandler<UpdateRssFeedCompletedEventArgs>(client_UpdateRssFeedCompleted);
            client.UpdateRssFeedAsync(feed, ((File)App.Current.Resources["File"]).FileID); 
        }

        void client_UpdateRssFeedCompleted(object sender, AsyncCompletedEventArgs e)
        {
            LoadCommand.Execute(null);
        }

        void client_GetRssFeedCompleted(object sender, GetRssFeedCompletedEventArgs e)
        {
            Feed = e.Result;
            IsChanged = false;
             
        }
    }
}
